<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>放大镜</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        font-size: 0;
      }
      div {
        display: inline-block;
        vertical-align: top;
      }
      .min {
        width: 300px;
        height: 300px;
        position: relative;
      }
      .minK {
        width: 100px;
        height: 100px;
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        background-color: rgba(0, 0, 0, 0.3);
      }
      .max {
        width: 900px;
        height: 900px;
        position: absolute;
        top: 0;
        left: 0;
      }
      .maxK {
        width: 300px;
        height: 300px;
        overflow: hidden;
        position: relative;
      }
    </style>
  </head>
  <body>
    <div class="min" id="min">
      <img class="min" src="./AOS2CON.png" alt="" />
      <div class="minK" id="minK"></div>
    </div>
    <div class="maxK" id="maxK">
      <img class="max" id="max" src="./AOS2CON.png" alt="" />
    </div>
  </body>
  <script>
    var min = document.getElementById("min");
    var minK = document.getElementById("minK");
    var maxK = document.getElementById("maxK");
    var max = document.getElementById("max");
    var x, y;
    min.onmouseenter = function () {
      minK.style.display = "block";
      min.onmousemove = function (e) {
        var e = e || window.event;
        x = e.clientX - this.offsetLeft;
        if (x > this.clientWidth - minK.clientWidth / 2) {
          x = this.clientWidth - minK.clientWidth / 2;
        }
        if (x < minK.clientWidth / 2) {
          x = minK.clientWidth / 2;
        }

        y = e.clientY - this.offsetTop;
        if (y > this.clientHeight - minK.clientHeight / 2) {
          y = this.clientHeight - minK.clientHeight / 2;
        }
        if (y < minK.clientHeight / 2) {
          y = minK.clientHeight / 2;
        }
        minK.style.left = x - minK.clientWidth / 2 + "px";
        minK.style.top = y - minK.clientHeight / 2 + "px";
        max.style.left =
          -(x - minK.clientWidth / 2) * (max.clientWidth / min.clientWidth) +
          "px";
        max.style.top =
          -(y - minK.clientHeight / 2) * (max.clientHeight / min.clientHeight) +
          "px";
      };
    };
  </script>
</html>
